package com.googlecode.cswish.struts.conversion;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.components.Select;

import com.googlecode.cswish.model.Code;
import com.googlecode.cswish.model.PageElement;
import com.googlecode.cswish.model.PropertyInfo;
import com.googlecode.cswish.util.CodeCategory;

/**
 * Convert select object
 * 	 
 * @author jerry.feng Date: 2009-1-13
 */
public class SelectElementConverter extends CommonElementConverter {
	
	@Resource
	private CodeCategory codeCategory;
	
	@Override
	public PageElement convert(ConversionContext context) {
		PropertyInfo propertyInfo = context.getPropertyInfo();
		Collection<Code> codes = codeCategory.getCodes(propertyInfo.getName(), true);
		boolean found = codes != null;
		if (found) {
			PageElement element = super.convert(context);
			
			String type = Select.class.getName();
			element.setType(type);
			Map<String, Object> extraParams = new HashMap<String, Object>(4);
			extraParams.put("list", codes);
			extraParams.put("listKey", "id");
			extraParams.put("listValue", "name");
			element.setExtraParams(extraParams);
			return element;
		}
		return null;
	}
}